﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Kokomo
{
	/// <summary>
	/// Provides static methods to assist with argument validation and throwing exceptions.
	/// </summary>
	public static class ArgumentValidation
	{
		/// <summary>
		/// Checks for a null argument.
		/// </summary>
		/// <param name="parameterName">Name of the parameter.</param>
		/// <param name="argument">The argument.</param>
		/// <exception cref="ArgumentNullException"><paramref name="argument"/> is <c>null</c>.</exception>
		public static void CheckNullArgument(string parameterName, object argument)
		{
			if (argument == null) throw new ArgumentNullException(parameterName);
		}
		public static void CheckRange(string parameterName, int argument, int minimum, int maximum)
		{
			if (argument < minimum || argument > maximum) throw new ArgumentOutOfRangeException(parameterName, argument, string.Format("Argument must be within the range [{0}, {1}]", minimum, maximum));
		}
		public static void CheckMinimum(string parameterName, int argument, int minimum)
		{
			if (argument < minimum) throw new ArgumentOutOfRangeException(parameterName, argument, string.Format("Argument must be greater than [{0}]", minimum));
		}
		public static void CheckMaximum(string parameterName, int argument, int maximum)
		{
			if (argument > maximum) throw new ArgumentOutOfRangeException(parameterName, argument, string.Format("Argument must be greater than [{0}]", maximum));
		}
		public static TValue Cast<TValue>(string parameterName, object value)
		{
			ArgumentValidation.CheckNullArgument("parameterName", value);
			if (!(value is TValue))
			{
				// TODO: Put this comment in the resource file
				throw new ArgumentException("Parameter must be of type " + typeof(TValue).FullName, parameterName);
			}

			return (TValue)value;
		}
	}
}
